import PageTemplates from "PageTemplates";
import { Common, Md5 } from "UtilsCommon";
const { EntityPageEdit } = PageTemplates;

const adminUserEdit = {
    expandInit() {
        this.menuRightsProperty = this.getProperty('MenuRights');
        this.rightGroupIds = this.getProperty('RightGroupIds');
        this.rightGroupIds.valueChange = this.rightGroupIdsChange.bind(this);
    },
    rightGroupIdsChange(value, selectDataList) {
        if (selectDataList.length === 0) this.menuRightsProperty.setValue([]);
        else {
            if (this.menuList) this.getRightGroupMenus(selectDataList);
            else this.getMenus(selectDataList);
        }
    },
    getRightGroupMenus(selectDataList) {
        this.dataCount = selectDataList.length;
        this.currentCount = 0;
        const dataList = Common.clone(this.menuList);
        selectDataList.forEach(d => {
            if (d.MenuRights) this.setMenuRight(dataList, d, d.MenuRights);
            else {
                const pathQuery = `?RightGroupId=${d.RightGroupId}`;
                this.dispatchAction("RightGroupService", "getRightGroupMenus", { pathQuery }).then(res => {
                    if (this.isSuccessProps(res)) {
                        d.MenuRights = res;
                        this.setMenuRight(dataList, d, d.MenuRights);
                    }
                    else this.alert(res.message);
                });
            }
        });
    },
    getMenus(selectDataList) {
        this.dispatchAction("MenuService", "getMenus", {}).then(res => {
            if (this.isSuccessProps(res)) {
                this.menuList = res;
                this.getRightGroupMenus(selectDataList);
            }
            else this.alert(res.message);
        });
    },
    setMenuRight(dataList, rightGroup, menuRights) {
        menuRights = menuRights || [];
        this.currentCount += 1;

        dataList.forEach(d => {
            for (let i = 0; i < menuRights.length; i++) {
                if (d.MenuKey === menuRights[i].MenuKey || d.NavMenuKey === menuRights[i].MenuKey) {
                    d.AccessRightName = "有";
                    if (d.DataRight === undefined) d.DataRight = -1;
                    if (d.OperationRight === undefined) d.OperationRight = -1;
                    if (d.DataRight < rightGroup.DataRight) {
                        d.DataRightName = rightGroup.DataRightName;
                        d.DataRight = rightGroup.DataRight;
                    }
                    if (d.OperationRight < rightGroup.OperationRight) {
                        d.OperationRightName = rightGroup.OperationRightName;
                        d.OperationRight = rightGroup.OperationRight;
                    }
                    break;
                }
            }
        });

        if (this.dataCount === this.currentCount) this.menuRightsProperty.setValue(dataList);
    },
    expandSetEntityData({ entityData, view }) {
        delete entityData.MenuRights;

        const isUpdate = view.entityData && view.entityData.UserId;
        if (!isUpdate && Common.isNullOrEmpty(entityData.LoginPassword)) {
            this.alert("登录密码不能为空！"); return;
        }

        if (!Common.isNullOrEmpty(entityData.LoginPassword) && !Common.isEquals(entityData.LoginPassword, entityData.LoginAgainPassword)) {
            this.alert("登录密码与确认密码不一致！");
            return false;
        }

        if (entityData.LoginPassword) entityData.LoginPassword = Md5(entityData.LoginPassword);
        else delete entityData.LoginPassword;

        return entityData;
    }
};

export default EntityPageEdit("adminUserEdit", "AdminUser", '用户-编辑', 300, adminUserEdit);
